package com.ias.agdis_r.activity.setting;

import com.ias.agdis_r.R;
import com.ias.agdis_r.activity.MainActivity;
import com.ias.agdis_r.manager.EffectManager;
import com.ias.agdis_r.manager.XmlManager;
import com.ias.agdis_r.module.ParameterModel;
import com.ias.agdis_r.util.S;
import com.ias.agdis_r.util.sdcard.SDcard;
import com.ias.agdis_r.util.UpdateUtil;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.preference.MultiSelectListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.RingtonePreference;
import android.preference.SwitchPreference;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 提示效果设置
 * 
 * @author qibo.yu
 *
 */
@SuppressWarnings("deprecation")
public class InfoEffectSettingsActivity extends PreferenceActivity {

	private final String PreferenceFileName = ParameterModel.T_VIBERATION_EFFECT;

	private TextView title;

	/** 收到消息时提示音 */
	private RingtonePreference ring_receive_info;
	/** 收到任务时提示音 */
	private RingtonePreference ring_receive_product;


	/** 语音播报设置 */
	private MultiSelectListPreference multiSelectListPreference;

	/** 收到消息时震动提示开关 */
	private SwitchPreference shake_receive_info;
	/** 收到任务时震动提示开关 */
	private SwitchPreference shake_receive_product;

	/** 提示音变动监听器 */
	private Preference.OnPreferenceChangeListener rintoneChangeListener = new OnPreferenceChangeListener() {

		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			initRingtoneSummary(preference, newValue.toString());
			return true;
		}
	};

	/** 震动开关变动监听器 */
	private Preference.OnPreferenceChangeListener vibratorSwitchChangeListener = new OnPreferenceChangeListener() {

		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			if (preference instanceof SwitchPreference && !((SwitchPreference) preference).isChecked()) {
				EffectManager.getInstance().vibrate();
			}
			return true;
		}
	};

	/** 语音播报变动监听器 */
	private Preference.OnPreferenceChangeListener multiSelectListPreferenceListener = new OnPreferenceChangeListener() {

		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			/**
			 * 一定时间后更新TTS配置
			 */
			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					EffectManager.getInstance().initTTS();
				}
			}, 900);
			return true;
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		initactionBar();
		setTitle(R.string.InfoEffectSettingsActivity);

		getPreferenceManager().setSharedPreferencesName(this.PreferenceFileName);
		addPreferencesFromResource(R.xml.info_effect);

		ring_receive_info = (RingtonePreference) findPreference(getString(R.string.ring_receive_info));
		ring_receive_product = (RingtonePreference) findPreference(getString(R.string.ring_receive_product));

		multiSelectListPreference = (MultiSelectListPreference) findPreference(
				getString(R.string.settings_voice_effect));

		shake_receive_info = (SwitchPreference) findPreference(getString(R.string.shake_receive_info));
		shake_receive_product = (SwitchPreference) findPreference(getString(R.string.shake_receive_product));

		SharedPreferences preferences = getPreferenceManager().getSharedPreferences();
		initRingtoneSummary(ring_receive_info, preferences.getString(ring_receive_info.getKey(), ""));
		initRingtoneSummary(ring_receive_product, preferences.getString(ring_receive_product.getKey(), ""));

		ring_receive_info.setOnPreferenceChangeListener(this.rintoneChangeListener);
		ring_receive_product.setOnPreferenceChangeListener(this.rintoneChangeListener);

		multiSelectListPreference.setOnPreferenceChangeListener(this.multiSelectListPreferenceListener);

		shake_receive_info.setOnPreferenceChangeListener(this.vibratorSwitchChangeListener);
		shake_receive_product.setOnPreferenceChangeListener(this.vibratorSwitchChangeListener);

	}

	/**
	 * 为RingtonePreference指定可理解的副标题，显示当前提示音
	 * 
	 * @param preference
	 * @param uri
	 */
	private void initRingtoneSummary(Preference preference, String uri) {
		String msg = getString(R.string.mute);
		if (S.isNotEmpty(uri)) {
			Ringtone ringtone = RingtoneManager.getRingtone(InfoEffectSettingsActivity.this, Uri.parse(uri));
			if (ringtone != null) {
				msg = ringtone.getTitle(InfoEffectSettingsActivity.this);
			}
		}
		preference.setSummary(msg);
	}

	@Override
	public void setTitle(CharSequence title) {
		this.title.setText(title);
		super.setTitle(title);
	}

	@Override
	public void setTitle(int titleId) {
		this.setTitle(getString(titleId));
	}

	@SuppressLint("InflateParams")
	private void initactionBar() {
		final Activity context = this;

		ActionBar actionBar = getActionBar();

		actionBar.setCustomView(LayoutInflater.from(this).inflate(R.layout.actionbar_settings, null));
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setDisplayShowCustomEnabled(true);

		this.title = (TextView) findViewById(R.id.txt_title);

		ImageView back = (ImageView) findViewById(R.id.actionbar_settings_back);
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@Override
	protected void onDestroy() {

		// 将文件拷贝到SD卡
		final String filePath = new StringBuilder(getFilesDir().getParentFile().getAbsolutePath())
				.append("/shared_prefs/").append(this.PreferenceFileName).append(".xml").toString();
		final String disPath = new StringBuilder(SDcard.getSDPATH()).append("/").append(XmlManager.packageName)
				.append("/chipor_info_effect.xml").toString();
		new Thread(new Runnable() {
			@Override
			public void run() {
				new UpdateUtil().copyFile(filePath, disPath);
			}
		}).start();

		super.onDestroy();
	}

}
